package cn.ibdsr.human.controller;

import cn.ibdsr.human.constant.BizConstants;
import cn.ibdsr.human.constant.Const;
import cn.ibdsr.human.constant.Result;
import cn.ibdsr.human.db.mapper.CompanyMapper;
import cn.ibdsr.human.db.mapper.UserMapper;
import cn.ibdsr.human.db.model.Company;
import cn.ibdsr.human.db.model.User;
import cn.ibdsr.human.exception.BizExceptionEnum;
import cn.ibdsr.human.exception.BusinessException;
import cn.ibdsr.human.manager.LogFormatManager;
import cn.ibdsr.human.service.CompanyService;
import cn.ibdsr.human.service.JobService;
import cn.ibdsr.human.transfer.JobStatusDTO;
import cn.ibdsr.human.transfer.UserDetailDTO;
import cn.ibdsr.human.transfer.UserDetailSaveDTO;
import cn.ibdsr.human.transfer.company.CompanyResponseDTO;
import cn.ibdsr.human.transfer.company.CompanySaveDTO;
import cn.ibdsr.human.transfer.company.CompanyStatusDTO;
import cn.ibdsr.human.transfer.company.UpdateConnectUserDTO;
import cn.ibdsr.human.transfer.job.*;
import cn.ibdsr.human.util.RegUtils;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

@Api(tags = "企业控制器")
@RestController
@RequestMapping(value = "/companies")
@Slf4j
@Validated
public class CompanyController {

    @Resource
    private CompanyService companyService;
    @Resource
    private JobService jobService;
    @Resource
    private UserMapper userMapper;
    @Resource
    private CompanyMapper companyMapper;

    @Resource
    private  LogFormatManager logFormatManager;
    /**
     * cdl-ok
     * 获取个人信息
     */
    @ApiOperation("获取企业信息")
    @GetMapping("/information")
    @ResponseBody//
    public Result<CompanyResponseDTO> getCompanyInformation(@RequestAttribute(name = Const.USER_ID  ) Long userId) {
        // Long userId=1L;
        logFormatManager.formatKeyPointLog("获取企业信息","userId="+userId, BizConstants.MODULE_COMPANY);
        CompanyResponseDTO companyResponseDTO=companyService.getCompanyInformation(userId);
        return new Result<>(companyResponseDTO);
    }

    /**
     * cdl-ok
     * 保存个人信息
     */
    @ApiOperation("保存企业信息/提交认证")
    @PutMapping
    @ResponseBody//@RequestAttribute(name = Const.USER_ID  )
    public Result<Long> save(@RequestAttribute(name = Const.USER_ID  ) Long userId,  @RequestBody @Valid CompanySaveDTO companySaveDTO) {
        logFormatManager.formatKeyPointLog("保存企业信息/提交认证","userId="+userId+":"+JSONObject.toJSONString(companySaveDTO), BizConstants.MODULE_COMPANY);
        Long flag=companyService.save(userId,companySaveDTO);
        return new Result<>(flag);
    }
    /**
     * cdl-ok
     * 保存个人信息
     */
    @ApiOperation("修改联系人")
    @PutMapping("/contact-users")
    @ResponseBody//@RequestAttribute(name = Const.USER_ID  )
    public Result<Long> updateConnectUser(@RequestAttribute(name = Const.USER_ID  ) Long userId,  @RequestBody @Valid UpdateConnectUserDTO updateConnectUserDTO) {
        logFormatManager.formatKeyPointLog("修改联系人","userId="+userId+":"+JSONObject.toJSONString(updateConnectUserDTO), BizConstants.MODULE_COMPANY);
        Long flag=companyService.updateConnectUser(userId,updateConnectUserDTO);
        return new Result<>(flag);
    }

    /**
     * cdl-ok
     * 保存个人信息
     */
    @ApiOperation("撤销认证和重新提交认证")
    @PutMapping("/statuses")//@RequestAttribute(name = Const.USER_ID  )
    @ResponseBody
    public Result<Long> saveCompanyStatus(@RequestAttribute(name = Const.USER_ID  ) Long userId,  @RequestBody @Valid CompanyStatusDTO companyStatusDTO) {
        logFormatManager.formatKeyPointLog("撤销认证和重新提交认证","userId="+userId+":"+JSONObject.toJSONString(companyStatusDTO), BizConstants.MODULE_COMPANY);
        Long flag=companyService.saveCompanyStatus(userId,companyStatusDTO);
        return new Result<>(flag);
    }

    /**
     * cdl-ok
     * 发布岗位
     */
    @ApiOperation("编辑岗位")
    @PutMapping("/jobs/{id}")//@RequestAttribute(name = Const.USER_ID  )
    @ResponseBody
    public Result<Long> editJob(@RequestAttribute(name = Const.USER_ID  ) Long userId, @PathVariable("id") Long id, @RequestBody @Valid JobSaveDTO jobSaveDTO) {
        jobSaveDTO.setId(id);
        logFormatManager.formatKeyPointLog("编辑岗位","userId="+userId+": "+JSONObject.toJSONString(jobSaveDTO), BizConstants.MODULE_COMPANY);
        Long flag = jobService.editJob(userId, jobSaveDTO);
        return new Result<>(flag);
    }

    /**
     * cdl-ok
     * 发布岗位
     */
    @ApiOperation("发布岗位（可选立即发布和暂不发布）")
    @PostMapping("/jobs")//@RequestAttribute(name = Const.USER_ID  )
    @ResponseBody
    public Result<Long> publishJob(@RequestAttribute(name = Const.USER_ID  )   Long userId,  @RequestBody @Valid JobSaveDTO jobSaveDTO) {
        logFormatManager.formatKeyPointLog("发布岗位（可选立即发布和暂不发布）","userId="+userId+":"+JSONObject.toJSONString(jobSaveDTO), BizConstants.MODULE_COMPANY);
        Long flag=jobService.publishJob(userId,jobSaveDTO);
        return new Result<>(flag);
    }
    /**
     * cdl-ok
     * 发布岗位
     */
    @ApiOperation("发布岗位/取消发布")
    @PutMapping("/jobs/publish-statuses/{id}")//@RequestAttribute(name = Const.USER_ID  )
    @ResponseBody
    public Result<Long> publishJobOrNot( @RequestAttribute(name = Const.USER_ID  )  Long userId,@PathVariable("id") Long id,   @RequestBody JobOperatorDTO jobSaveDTO) {
        jobSaveDTO.setId(id);
        logFormatManager.formatKeyPointLog("发布岗位/取消发布","userId="+userId+":"+JSONObject.toJSONString(jobSaveDTO), BizConstants.MODULE_COMPANY);
        Long flag=jobService.publishJobOrNot(userId,jobSaveDTO);
        return new Result<>(flag);
    }

    /**
     * cdl-ok
     * 发布岗位
     */
    @ApiOperation("删除岗位")
    @DeleteMapping("/jobs/{id}")//@RequestAttribute(name = Const.USER_ID  )
    @ResponseBody
    public Result<Long> delete( @RequestAttribute(name = Const.USER_ID  )  Long userId,@PathVariable("id") Long id) {
        logFormatManager.formatKeyPointLog("删除岗位","userId="+userId+": id="+id, BizConstants.MODULE_COMPANY);
        Long flag=jobService.delete(userId,id);
        return new Result<>(flag);
    }

    /**
     * cdl-ok
     * 发布岗位
     */
    @ApiOperation("获取岗位详情")
    @GetMapping("/jobs/{id}")//@RequestAttribute(name = Const.USER_ID  )
    @ResponseBody
    public Result<JobResponseDTO> getJobDetail(@RequestAttribute(name = Const.USER_ID  ) Long userId, @PathVariable("id") Long id ) {
        logFormatManager.formatKeyPointLog("获取岗位详情","userId="+userId+": id="+id, BizConstants.MODULE_COMPANY);
        JobResponseDTO flag=jobService.getJobDetail(id);
        return new Result<>(flag);
    }
    /**
     * cdl-ok
     * 发布岗位
     */
    @ApiOperation("企业分页查询岗位信息")
    @GetMapping("/jobs")//@RequestAttribute(name = Const.USER_ID  )
    @ResponseBody
    public Result<JobListResponseDTO> getJobList(@RequestAttribute(name = Const.USER_ID  )  Long userId, @Valid JobQueryDTO jobQueryDTO ) {
        logFormatManager.formatKeyPointLog("企业分页查询岗位信息","userId="+userId+":"+JSONObject.toJSONString(jobQueryDTO), BizConstants.MODULE_COMPANY);
        User user = userMapper.selectById(userId);
        if (user == null) {
            throw new BusinessException(BizExceptionEnum.USER_NOT_EXISTED);
        }
        if (user.getCompanyId() == null) {
            throw new BusinessException(BizExceptionEnum.COMPANY_NOT_IDENTIFICATION);
        }
        Company company = companyMapper.selectById(user.getCompanyId());
        if (company == null) {
            throw new BusinessException(BizExceptionEnum.COMPANY_NOT_IDENTIFICATION);
        }
        jobQueryDTO.setCompanyId(company.getId());
        JobListResponseDTO flag=jobService.getJobList(jobQueryDTO);
        return new Result<>(flag);
    }


}
